package afcp.alumni.file;

//import java.io.Closeable;
//import java.io.IOException;
//import java.io.InputStream;
//import java.text.ParseException;
//import java.util.Date;
//import java.util.Iterator;
//import java.util.List;
//
//import org.apache.commons.logging.Log;
//import org.apache.commons.logging.LogFactory;
//
//import afcp.alumni.dao.RoleDao;
//import afcp.alumni.model.Pb;
//import afcp.alumni.model.db.FileColumn;
//import afcp.alumni.model.db.Member;
//import afcp.alumni.model.db.Member.Sex;
//import afcp.alumni.model.db.MemberUtils;
//
///**
// * 
// * @author aorylamballe
// * @deprecated Dans le cas d'un chargement de fichier Excel, nécessite
// *             d'utiliser un {@link ExcelReader} qui charge le fichier d'un
// *             coup. Si le fichier Excel est trop gros, cause des problèmes de
// *             mémoire.
// */
//@Deprecated
//public class MemberReader implements Iterable<Member>, Closeable {
//
//	private static final Log logger = LogFactory.getLog(MemberReader.class);
//	
////	private RoleDao roleDao;
//
////	private Map<String, String> paramHeaders;
//	private List<FileColumn> columns;
//	
//	private FileReader reader;
//	
//	public MemberReader(InputStream inputStream, AlumniFile file, RoleDao roleDao)
//			throws IncorrectHeaderException, IOException {
////		this.paramHeaders = file.getHeaders();
//		
//		this.columns = file.getHeaders();
////		List<FileColumn> columns = file.getHeaders();
////		columnNames = new ArrayList<>(columns.size());
////		for (FileColumn column : columns) {
////			columnNames.set(column.getPosition(), column.getId());
////		}
//		
//		this.reader = file.toFileReader(inputStream);
////		this.roleDao = roleDao;
//		checkHeaders();
//	}
//
//	private static final int IDX_FIRSTNAME = 0;
//	private static final int IDX_LASTNAME = 1;
////	private static final int IDX_LOGIN = 2;
////	private static final int IDX_PASSWORD = 3;
//	private static final int IDX_SEX = 4;
//	private static final int IDX_CHINESE_NAME = 5;
//	private static final int IDX_BIRTH_DATE = 6;
//	private static final int IDX_COUNTRY = 7;
//	private static final int IDX_EMAIL = 8;
//	private static final int IDX_PHONE = 9;
//	private static final int IDX_WEIBO = 10;
//	private static final int IDX_WEIXIN = 11;
//	private static final int IDX_PARISTECH_ENTRANCE_YEAR = 12;
//	private static final int IDX_PARISTECH_SCHOOL = 13;
//	private static final int IDX_CHINESE_UNIVERSITY = 14;
//	private static final int IDX_EMPLOYER = 15;
//	private static final int IDX_CURRENT_JOB_COUNTRY = 16;
//	private static final int IDX_JOB = 17;
//	private static final int L=18;
//	private int[] idx = new int[L];
//
//	private int lastIndex = -1;
//
//	private void checkHeaders() throws IOException, IncorrectHeaderException {
//		// initialize idx array with -1
//		for (int i=0; i<L ; i++) {
//			idx[i] = -1;
//		}
//		
////		String[] headers = reader.readHeader(); // TODO enlever
//		reader.readHeader();
//
////		Map<String, String> expectedHeaders = paramHeaders;
////		Map<String, String> invExpHeaders = new HashMap<>();
////		for (Entry<String, String> e : expectedHeaders.entrySet()) {
////			invExpHeaders.put(e.getValue(), e.getKey());
////		}
//
////		int i = 0;
////		for (String header : headers) {
//		for (FileColumn column : columns) {
////			String value = invExpHeaders.get(header);
//			String value = column.getId();
//			if (value != null) {
//				switch (value) {
//				case MemberUtils.COL_FIRSTNAME:
//					idx[IDX_FIRSTNAME] = column.getPosition();
//					break;
//				case MemberUtils.COL_LASTNAME:
//					idx[IDX_LASTNAME] = column.getPosition();
//					break;
////				case MemberUtils.COL_LOGIN:
////					idx[IDX_LOGIN] = column.getPosition();
////					break;
////				case MemberUtils.COL_PASSWORD:
////					idx[IDX_PASSWORD] = column.getPosition();
////					break;
//				case MemberUtils.COL_SEX:
//					idx[IDX_SEX] = column.getPosition();
//					break;
//				case MemberUtils.COL_CHINESE_NAME:
//					idx[IDX_CHINESE_NAME] = column.getPosition();
//					break;
//				case MemberUtils.COL_BIRTH_DATE:
//					idx[IDX_BIRTH_DATE] = column.getPosition();
//					break;
//				case MemberUtils.COL_COUNTRY:
//					idx[IDX_COUNTRY] = column.getPosition();
//					break;
//				case MemberUtils.COL_EMAIL:
//					idx[IDX_EMAIL] = column.getPosition();
//					break;
//				case MemberUtils.COL_PHONE:
//					idx[IDX_PHONE] = column.getPosition();
//					break;
//				case MemberUtils.COL_WEIBO:
//					idx[IDX_WEIBO] = column.getPosition();
//					break;
//				case MemberUtils.COL_WEIXIN:
//					idx[IDX_WEIXIN] = column.getPosition();
//					break;
//				case MemberUtils.COL_PARISTECH_ENTRANCE_YEAR:
//					idx[IDX_PARISTECH_ENTRANCE_YEAR] = column.getPosition();
//					break;
//				case MemberUtils.COL_PARISTECH_SCHOOL:
//					idx[IDX_PARISTECH_SCHOOL] = column.getPosition();
//					break;
//				case MemberUtils.COL_CHINESE_UNIVERSITY:
//					idx[IDX_CHINESE_UNIVERSITY] = column.getPosition();
//					break;
//				case MemberUtils.COL_EMPLOYER:
//					idx[IDX_EMPLOYER] = column.getPosition();
//					break;
//				case MemberUtils.COL_CURRENT_JOB_COUNTRY:
//					idx[IDX_CURRENT_JOB_COUNTRY] = column.getPosition();
//					break;
//				case MemberUtils.COL_JOB:
//					idx[IDX_JOB] = column.getPosition();
//					break;
//				}
//			}
////			i++;
//		}
//		lastIndex = max(idx);
//
////		// TODO compléter avec les données obligatoires
////		if (idx[IDX_FIRSTNAME] == -1 || idx[IDX_LASTNAME] == -1
////				|| idx[IDX_BIRTH_DATE] == -1) {
////			throw new IncorrectHeaderException(headers,
////					columns);
////		}
//	}
//	
//	private int max(int... numbers) {
//		if (numbers == null) {
//			return 0;
//		}
//		int l = numbers.length;
//		if (l == 0) {
//			return 0;
//		}
//		int result = numbers[0];
//		for (int i = 1; i < l; i++) {
//			int n = numbers[i];
//			if (n > result) {
//				result = n;
//			}
//		}
//		return result;
//	}
//
////	private MemberService memberService = SF.member();
//
//	private boolean iterated = false;
//
//	@Override
//	public Iterator<Member> iterator() {
//		if (iterated) {
//			throw new UnsupportedOperationException(
//					"Can't get two iterators of MemberCSVReader. "
//					+ "This reader can be read only once.");
//		}
//		iterated = true;
//		return new MemberCsvIterator();
//	}
//
//	@Override
//	public void close() throws IOException {
//		reader.close();
//	}
//	private static boolean empty(String str) {
//		return str == null || str.isEmpty();
//	}
//
//	private class MemberCsvIterator implements Iterator<Member> {
//
//		private Member next = null;
//
//		@Override
//		public boolean hasNext() {
//			if (next == null) {
//				next = readNextMember();
//			}
//			return next != null;
//		}
//
//		private long i = 0;
//
//		private Member readNextMember() {
//			String[] line = null;
//			try {
//				boolean loop = false;
//				do {
//					i++;
//					line = MemberReader.this.reader.readNext();
//					if (line == null) {
//						return null;
//					}
//					loop = line.length < lastIndex;
//					if (loop) {
//						logger.error("La ligne " + i
//								+ " du fichier a un format incorrect. "
//								+ "Contenu trouvé pendant l'analyse: "
//								+ lineArrayToString(line));
//					}
//				} while (loop);
//			} catch (IOException e) {
//				logger.error(
//						"A problem occurred while trying to read the member "
//						+ "on the uploaded file or while trying to add it "
//						+ "to the datastore.",
//						e);
//				return null;
//			}
//			
//			// TODO reporter dans un MemberParser
//			Member member = new Member();
//			String firstName = col(line, IDX_FIRSTNAME);
//			String lastName = col(line, IDX_LASTNAME);
//			String sexStr = col(line, IDX_SEX);
//			Sex sex = MemberUtils.toSex(sexStr);
//			if (sexStr != null && sex == null) {
//				member.addWarning(Pb.INCORRECT_SEX);
//			}
//			String chineseName = col(line, IDX_CHINESE_NAME);
//			Date birthDate;
//			String birthDateStr = null;
//			try {
//				birthDateStr = col(line, IDX_BIRTH_DATE);
//				birthDate = MemberUtils.toDate(birthDateStr);
//			} catch (ParseException e) {
////				birthDateStr = null;
//				birthDate = null;
//			}
//			String country = col(line, IDX_COUNTRY);
//			String email = col(line, IDX_EMAIL);
//			String phone = col(line, IDX_PHONE);
//			String weibo = col(line, IDX_WEIBO);
//			String weixin = col(line, IDX_WEIXIN);
//			int parisTechEntranceYear = -1;
//			try {
//				parisTechEntranceYear = MemberUtils.toYear(col(line, IDX_PARISTECH_ENTRANCE_YEAR));
//			} catch (NullPointerException e) {
//				member.addError(Pb.NULL_YEAR_VALUE);
//			} catch (NumberFormatException e) {
//				member.addError(Pb.INCORRECT_YEAR_NUMBER_FORMAT);
//			}
//			String parisTechSchool = col(line, IDX_PARISTECH_SCHOOL);
//			String chineseUniversity = col(line, IDX_CHINESE_UNIVERSITY);
//			String employer = col(line, IDX_EMPLOYER);
//			String currentJobCountry = col(line, IDX_CURRENT_JOB_COUNTRY);
//			String job = col(line, IDX_JOB);
//
////			String loginId = col(line, IDX_LOGIN);
////			Login login;
////			if (loginId != null) {
////				login = new Login(loginId, member);
////			} else if (email != null && !email.isEmpty()) {
////				login = new Login(email, member);
////			} else {
////				login = new Login(firstName + lastName + birthDateStr, member);
////			}
////			String password = col(line, IDX_PASSWORD);
//			
//			if (empty(firstName)) {
//				member.addError(Pb.NO_FIRSTNAME);
//			}
//			if (empty(lastName)) {
//				member.addError(Pb.NO_LASTNAME);
//			}
//			if (empty(birthDateStr)) {
//				member.addError(Pb.NO_BIRTHDATE);
//			} else if (birthDate == null) {
//				member.addError(Pb.INCORRECT_BIRTHDATE_FORMAT);
//			}
////			Login login = null;
////			if (member.getProblems() == null) {
////				login = new Login(firstName + lastName + birthDateStr, member);
////			}
//			
//			
//			member.setAll(firstName, lastName, sex, chineseName,
//					birthDate, country, email, phone, weibo, weixin,
//					parisTechEntranceYear, parisTechSchool, chineseUniversity,
//					employer, currentJobCountry, job);
////			Login login = new Login(member);
////			member.setLogin(login);
//
//			return member;
//		}
//		
//		private String col(String[] line, int index) {
//			int lineIdx = idx[index];
//			if (lineIdx < 0) {
//				return null;
//			} else {
//				return line[lineIdx];
//			}
//		}
//
//		private String lineArrayToString(String[] line) {
//			StringBuilder lineBuilder = new StringBuilder();
//			boolean first = true;
//			for (String col : line) {
//				if (first) {
//					lineBuilder.append(", ");
//				} else {
//					first = false;
//				}
//				lineBuilder.append(col);
//			}
//			return lineBuilder.toString();
//		}
//
//		@Override
//		public Member next() {
//			if (next != null) {
//				// if hasNext() has been called.
//				Member result = next;
//				next = null;
//				return result;
//			} else {
//				return readNextMember();
//			}
//		}
//
//		@Override
//		public void remove() {
//			throw new UnsupportedOperationException(
//					"Can't remove entries in uploaded file.");
//		}
//
//	}
//
//}
